1
Beyond Linear Search: The Power of Associative Containers
AI037 Lesson 15
00:00

Imagine a library where books aren't shelved by arrival date, but by a Universal Key. This is the paradigm shift from sequential to Associative Containers. Instead of scanning a vector linearly ($O(N)$), we utilize a map or set to achieve logarithmic-time lookups ($O(\log n)$).

1. The Nature of Association

In a map, we store key-value pairs. The key acts as an index that can be a string, a custom object, or any type supporting Strict Weak Ordering. A set, conversely, stores only unique keys, making it the perfect tool for membership testing or filtering.

Vector (Sequential) Set (Associative) [0] "A" [1] "B" [2] "A" (Dup) Unique Filter Key: "A" Key: "B"

2. Ordered vs. Unordered

Standard containers (map, set) keep keys sorted. The C++11 Standard introduced unordered variants (unordered_map) that use a hash function for average $O(1)$ performance. Look for the C++11 Logo when utilizing these high-performance buckets.

main.py
TERMINAL bash — 80x24
> Ready. Click "Run" to execute.
>